#!/usr/bin/env python

BASE = '/home/activist.ca/database/backup'
OUTPUTDIR = '/home/mike/projects/activist.ca/groupdump'
BOILER = '''
Greetings,

You have been listed as a contact for the following group
information on activist.ca.

We are in the process of re-implementing the Web site, which
will be re-launched on January 28, 2005. Before the re-launch,
we wish to confirm all group information, and will delete any
that is out of date.

If you could take a few minutes to confirm that the following
information is correct by replying to this email and making any
changes necessary, we will include your group in the re-launched
site. If we do not hear back from you BEFORE January 15th, 2005,
we will presume your group in inactive.

Should you miss this deadline, you may of course re-add your group
to the site at any time.

Thanks,
activist.ca collective

'''


import os
listing = os.path.join(BASE,'listing.txt')
joiner = os.path.join(BASE,'listing_listing.txt')
detailfile = os.path.join(BASE,'listingdetail.txt')

def wordwrap(text, width=70):
        """
        from aspn
        
        A word-wrap function that preserves existing line breaks
        and most spaces in the text. Expects that existing line
        breaks are posix newlines (\n).
        """
        return reduce(lambda line, word, width=width: '%s%s%s' %
                      (line,
                       ' \n'[(len(line)-line.rfind('\n')-1
                              + len(word.split('\n',1)[0]
                                    ) >= width)],
                       word),
                      text.split(' ')
                      )

join = {}
for line in open(joiner,'r').readlines():
    things = line.split('\t')
    key = things[0] + '-' + things[1]
    value = things[2] + '-' + things[3]
    if join.has_key(key):
        join[key].append(value)
    else:
        join[key] = [value]


detail = {}

if 1:
    eat = False
    data = open(detailfile,'r').read()
    newdata = filter(lambda a: a != '\r', data)

    line = ''
    for l in newdata.split('\n'):
        if len(l) == 0: continue
        if eat:
            line = line + l + '\n'
            eat = False
            continue

        if l[-1] == '\\':
            eat = True
            line = line + l[:-1] + '\n'
            continue

        line = line + l
        things = line.split('\t')
        key = things[0] + '-' + things[1]
        if things[30] == '0': things[30] = ''
        if things[31] == '0': things[31] = ''
        list =  { 'title':things[8],
                  'subtitle':things[9],
                  'url':things[10],
                  'description':things[13],
                  'short':things[15],
                  'notes':things[16],
                  'cost':things[20],
                  'agerestriction':things[21],
                  'address':things[23],
                  'person':things[24],
                  'phone0':things[25],
                  'phone1':things[26],
                  'fax':things[27],
                  'email':things[28],
                  'image_url':things[29],
                  'image_width':things[30],
                  'image_height':things[31],
                  'posted_by':things[35]
                  }
        detail[key] = list
        line = ""
    

types = []
i = 0
for line in open(listing,'r').readlines():
    things = line.split('\t')
    if things[8].lower() == 'calgary' and things[14] == 'event':
        continue
        print things[0],things[1],things[14],things[15],things[16],things[18],things[21]
        try:
            key = join[things[0]+'-'+things[1]]
            print "   -->",key
            for x in key:
                if detail.has_key(x):
                    print x,":",str(detail[x])
                else:
                    print x,":","NONE"
        except KeyError:
            print "NO joined events"
        if not things[15] in types:
            types.append(things[15])
        i = i + 1

    elif things[14] == 'group':
        id = things[0] + '-' + things[1]
        if not join.has_key(id):
            continue
        i = i + 1
        listingtext = ''
        listingtext = listingtext + "<listing id=\"%s\">\n" % (id,)
        if join.has_key(id):
            keys = join[id]
            for x in keys:
                if detail.has_key(x):
                    listingtext = listingtext + "  <detail id=\"%s\">\n" % (x,)
                    for y in detail[x].keys():
                        if len(detail[x][y].strip()) == 0: continue
                        if y=='short' or y=='description':
                            text = '\n' + wordwrap(detail[x][y].strip()) + '\n'
                            t = ''
                            for line in text.split('\n'):
                                t = t + '        ' + line + '\n'
                            text = t[:-1] + '     '
                        else:
                            text = detail[x][y].strip()
                        listingtext = listingtext + "    <%s>%s</%s>\n" % (y,text,y)
                    listingtext = listingtext + "  </detail>\n"
        listingtext = listingtext + "</listing>\n\n\n"
        fn = os.path.join(OUTPUTDIR,things[1]+'.xml')
        print "writing",fn
        file = open(fn,'w')
        file.write(listingtext)
        file.close()

        fn = os.path.join(OUTPUTDIR,things[1]+'.email')
        print "writing",fn
        file = open(fn,'w')
        file.write(BOILER + listingtext)
        file.close()
    
        
print i,"listings for calgary"
print "types:",types
